package com.example;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MyActivity extends Activity implements View.OnClickListener {
    /**
     * Called when the activity is first created.
     */

    EditText fieldOne;
    TextView textOne;
    Button buttonAction;
    Button secondButton;
    Button alertButton;
    Button alertDialogSecond;

    /*
    TODO Dialog variables
     */

    private static final int ALERT_DIALOG_ONE = 0x123354;
    private static final int ALERT_DIALOG_SECOND = 0x36654;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        requestWindowFeature(Window.FEATURE_NO_TITLE);
//        getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.main);
        fieldOne = (EditText) findViewById(R.id.textGet);
        textOne = (TextView) findViewById(R.id.textOne);
        buttonAction = (Button) findViewById(R.id.buttonAction);
        secondButton = (Button) findViewById(R.id.buttonSecondAction);
        alertButton = (Button) findViewById(R.id.buttonAlertDialog);
        alertDialogSecond = (Button) findViewById(R.id.buttonAlertDialogSecond);

        secondButton.setOnClickListener(this);
        buttonAction.setOnClickListener(this);
        alertButton.setOnClickListener(this);
        alertDialogSecond.setOnClickListener(this);
    }


    public void onLog() {
        Log.e("LOG TEXT", fieldOne.getText().toString().trim());
        Log.e("LOG TEXT", fieldOne.getText().toString().trim());

    }

    public void onClear() {
        fieldOne.setText("");
    }

    public void onNext() {
        Intent i = new Intent(MyActivity.this, SecondActivity.class);
        startActivity(i);
    }

    public void onSecond() {
        Intent i = new Intent(MyActivity.this, ListViewForm.class);
        startActivity(i);
    }

    /*
    TODO Button action
     */
    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.buttonAction:
                onLog();
                break;
            case R.id.buttonSecondAction:
                onClear();
                break;
            case R.id.buttonAlertDialog:
                showDialog(ALERT_DIALOG_ONE);
                break;
            case R.id.buttonAlertDialogSecond:
                showDialog(ALERT_DIALOG_SECOND);
                break;
        }
        //To change body of implemented methods use File | Settings | File Templates.
    }

    /*
    TODO Dialog builder
     */

    protected Dialog onCreateDialog(int id) {
        Dialog dialog;
        AlertDialog.Builder builder;
        switch (id) {
            case ALERT_DIALOG_ONE:
                builder = new AlertDialog.Builder(this);
                builder.setMessage("ALERT ONE: Close this application?");
                builder.setCancelable(false);
                builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        android.os.Process.killProcess(android.os.Process.myPid());
                    }
                });

                builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        dialogInterface.cancel();
                    }
                });
                dialog = builder.create();
                break;
            case ALERT_DIALOG_SECOND:
                builder = new AlertDialog.Builder(this);
                builder.setMessage("ALERT SECOND: Close this application?");
                builder.setCancelable(false);
                builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        android.os.Process.killProcess(android.os.Process.myPid());
                    }
                });

                builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        dialogInterface.cancel();
                    }
                });
                dialog = builder.create();
                break;
            default:
                dialog = null;
        }
        return dialog;
    }

    /*
    TODO Menu
     */
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.main_menu, menu);
        return true;
    }

    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle item selection
        switch (item.getItemId()) {
            case R.id.nextForm:
                onNext();
                return true;
            case R.id.secForm:
                onSecond();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

}
